package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import domain.Product;
import service.Factory.ServiceFactory;

/**
 * Servlet implementation class AddShoppingCartServlet
 */
@WebServlet("/AddShoppingCartServlet")
public class AddShoppingCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddShoppingCartServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		Product p = new Product();

		p.setId(request.getParameter("id"));
		p.setImgurl(request.getParameter("imgurl"));
		p.setName(request.getParameter("PName"));
		p.setPrice(Double.parseDouble(request.getParameter("Price")));
		p.setPnum(Integer.parseInt(request.getParameter("PNum")));
		p.setSelct(1);
		
		List<Product> shoppingCart = (List<Product>) session.getAttribute("shoppingCart");
		
		session.setAttribute("shoppingCart", ServiceFactory.getShoppingCartServiceInstance().addShoppingCart(shoppingCart, p));
		
		response.sendRedirect(request.getContextPath() + "/client/Cart.jsp");
	}

}
